package com.example.demo.Impl;

import com.example.demo.entity.Student;
import com.example.demo.repository.StudentRepository;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentRepository studentRepository;

    @Override
    public Optional<Student> findByStudentNo(String studentNo) {
        return studentRepository.findByStudentNo(studentNo);
    }

    @Override
    public Student registerStudent(Student student) {
        // 注册前可以加校验，如学号是否已存在
        Optional<Student> existing = studentRepository.findByStudentNo(student.getStudentNo());
        if (existing.isPresent()) {
            throw new RuntimeException("该学号已被注册");
        }
        // 直接保存
        return studentRepository.save(student);
    }

    @Override
    public Optional<Student> login(String studentNo, String password) {
        Optional<Student> studentOpt = studentRepository.findByStudentNo(studentNo);
        if (studentOpt.isPresent()) {
            Student student = studentOpt.get();
            // 简单密码匹配，建议后续加密处理
            if (student.getPassword().equals(password)) {
                return Optional.of(student);
            }
        }
        return Optional.empty();
    }
}
